package local.app;


import com.jcraft.jsch.JSchException;
import local.app.server_audits.ServerChecker;
import local.app.servers.*;

import java.io.*;
import java.net.URISyntaxException;
import java.util.ArrayList;

public class App_test {

    public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException, JSchException {
        ArrayList<Server> servers = new ArrayList<>();
        String jarPath = App_test.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
        String file = new File(jarPath).getParent() + File.separator;

        try (
                BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file + "list_result", true))
        ) {
            servers.add(new OtuServer("ОТУ", "192.168.2.201"));
            servers.add(new RCodServer("РЦОД", "192.168.2.203"));
            servers.add(new StandardServer("Эмулятор Гознака", "192.168.1.3"));


            fileWriter.write("--------------------------------------");


            for (Server server :
                    servers) {
                System.out.println("Идет проверка - " + server.getNameServer() + " " + server.getIpAddress());
                StringBuilder result = new StringBuilder();
                ServerChecker checker = new ServerChecker(server);
                result.append("\n").append(server.getNameServer()).append("\n");
                result.append(server.getIpAddress()).append("\n");
                // Определение доступности сервера по IP-адресу с помощью ping
                result.append("Доступность сервера: ").append(checker.pingServer() ? "доступен" : "недоступен");

                // Выполнение аутентификации через SSH
                result.append("\nSSH authentication: ").append(checker.authenticateSSH() ? "successful" : "failed");

                // Получение версии операционной системы
                result.append("\nOS info: ").append(checker.getOSVersion());


                result.append("\n--------------------------------------");

                try {
                    System.out.printf("====== Идет запись ======\n" +
                                    "====== %s ======\n====== %s ======%n%n",
                            server.getNameServer(), server.getIpAddress());
                    fileWriter.write(result.toString());
                } catch (IOException e) {

                    System.out.printf("====== Ошибка записи ======\n" +
                                    "====== %s ======\n====== %s ======%n%n",
                            server.getNameServer(), server.getIpAddress());
                    throw new RuntimeException(e);

                } finally {
                    System.out.printf("====== Закончена проверка ======\n" +
                                    "====== %s ======\n====== %s ======%n%n",
                            server.getNameServer(), server.getIpAddress());
                }

            }
        }
    }
}
